var express = require('express');
var router = express.Router();
var connection = require('./dao').connection;

/* 获取微服务列表. */
router.get('/getAppList', function(req, res, next) {
    var sql='select app.app_id,app.name from app ';
    connection.query(sql, function (error, results) {
        if(error){
            console.log('[SELECT ERROR] - ',err.message);
        }
        console.log(results);
        res.end(JSON.stringify(results))

    });
});

/*获取对应版本以及对应微服务下的配置文件列表*/
router.get('/getConfigList',function (req,res,next) {
    var sql='select config.config_id,config.name from config where version=? and app_id=? ';
    var sqlParams=[req.query.version,req.query.appId];
    connection.query(sql, sqlParams,function (error, results) {
        if(error){
            console.log('[SELECT ERROR] - ',err.message);
            return;
        }
        console.log(results);
        res.end(JSON.stringify(results))
    });
});

/*获取对应版本、对应微服务、对应配置项下的配置值*/
router.get('/getConfigValue',function (req,res,next) {
    var sql='select config.value from config where version=? and app_id=? and config_id=?';
    var sqlParams=[req.query.version,req.query.appId,req.query.configId];
    connection.query(sql, sqlParams,function (error, results) {
        if(error){
            console.log('[SELECT ERROR] - ',err.message);
            return;
        }
        console.log(results);
        res.end(JSON.stringify(results))
    });
    }
);

/*获取matrix上的所有版本*/
router.get('/getAllVersion',function (req,res,next) {
        var allVersion=[];
        var sql='select DISTINCT(config.version) from config';
        connection.query(sql,function (error, results) {
            if(error){
                console.log('[SELECT ERROR] - ',err.message);
                return;
            }
            console.log(results);
            for(var i=0;i<results.length;i++){
                allVersion.push(results[i].version)
            }
            res.end(JSON.stringify(allVersion))
        });
    }
);

module.exports = router;